package com.qd.panda.mall;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.repeat.annotation.RepeatSubmit;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.stroke.PayParamDTO;
import com.qd.common.sys.enums.RemoteEnum;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.mall.MallOrderDTO;
import com.qd.panda.service.mall.MallService;
import com.qd.panda.service.stroke.DisplayUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 商城/订单/APP_订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/mall/")
@Slf4j
public class AppMallController {

    private final MallService mallService;

    /**
     * 创建订单
     *
     * @param mallOrderDTO 请求参数
     * @return 支付字段
     */
    @RepeatSubmit
    @PostMapping(value = "/createOrder")
    public ApiResultWrapper<Object> createOrder(@Validated({DefaultGroup.class}) @RequestBody MallOrderDTO mallOrderDTO) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WX_APP.getCode())) {
            log.error("创建订单,没有权限操作,remote: {}",SecurityUtils.getLoginUser().getRemote());
            return ApiResult.error(401, "没有权限操作");
        }
        PayParamDTO order = mallService.createOrder(mallOrderDTO);
        mallOrderDTO.freeData();
        return ApiResult.success(order);
    }

    /**
     * 支付失败
     *
     * @param map 业务ID
     */
    @RepeatSubmit
    @PostMapping("/payError")
    public ApiResultWrapper<Object> payError(@Validated @RequestBody Map<String, Object> map) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WX_APP.getCode())) {
            log.error("支付失败,没有权限操作,remote: {}",SecurityUtils.getLoginUser().getRemote());
            return ApiResult.error(401, "没有权限操作");
        }
        Object bid = map.get("thirdPartyOrder");
        Object payStatusObj = map.get("payStatus");
        if (null == bid) {
            return ApiResult.error("业务ID不能为空");
        }
        if (null == payStatusObj) {
            return ApiResult.error("支付状态不能为空");
        }
        int payStatus;
        try {
            payStatus = (int) payStatusObj;
        } catch (Exception e) {
            return ApiResult.error("支付状态值转换错误");
        }
        if (payStatus != DisplayUtil.PAY_STATUS_2 && payStatus != DisplayUtil.PAY_STATUS_4) {
            log.error("支付状态值错误: {}", payStatus);
            return ApiResult.error("支付状态值错误");
        }
        return mallService.payError(bid.toString(), payStatus);
    }

    /**
     * 支付详情
     *
     * @param thirdPartyOrder 业务ID
     * @return 详情
     */
    @RepeatSubmit
    @GetMapping("/payDetail")
    public ApiResultWrapper<Object> payDetail(@Validated @NotNull(message = "业务ID不能为空") String thirdPartyOrder) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WX_APP.getCode())) {
            return ApiResult.error(401, "没有权限操作");
        }
        return ApiResult.success(mallService.payDetail(thirdPartyOrder));
    }
}
